<?php
/*
 * Copyright 2007 Bjørn Tore Håvie
 * 
 * This file is part of ITSBOT.
 * 
 * ITSBOT is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ITSBOT is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ITSBOT.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * @package ITSBOT
 * @subpackage Log
 * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License
 */
class Log{
	
	private static $inst;
	
	private $file;
	
	private $debug;
	
	private $mode = array();
	
	public static function Get(){
		if(Log::$inst)
			return Log::$inst;
		Log::$inst = new Log();
		return Log::$inst;
	}
	
	private function __construct(){
		$this->file = fopen('logs/log.txt', 'a+');
		$this->Write("----------[START SESSION]----------\n");
	}
	
	function __destruct(){
		$this->Write("----------[END SESSION]----------\n");
		fclose($this->file);
	}
	
	private function Write($txt){
		fwrite($this->file, $txt);
	}
	
	public function EnableDebug($b){
		$this->debug = (bool)$b;
	}
	
	private function Format($level, $msg){
		$tmp = debug_backtrace();
		$msg = str_replace(array("\n", "\r"), '', $msg);
		return "[" . date("H:i:s") . "]$level(" . $tmp[2]['class'] . '::' . $tmp[2]['function'] . "): $msg\n";
	}
	
	function Debug($sender, $msg){
		if($this->debug){
			$txt = $this->Format("DEBUG", $msg);
			print($txt);
			$this->Write($txt);
		}
		return $msg;
	}
	
	function Notice($msg){
		$txt = $txt = $this->Format("NOTICE", $msg);
		print($txt);
		$this->Write($txt);
	}
	
	function Warning($msg){
		$txt = $this->Format("WARNING", $msg);
		print($txt);
		$this->Write($txt);
	}
	
	function Error($msg){
		$txt = $this->Format("ERROR", $msg);
		print($txt);
		$this->Write($txt);
	}
}
?>